package com.guomei.controller.curse;

import com.guomei.bean.curse.CourseVideo;
import com.guomei.bean.curse.CreateOrUpdateCourseVideo;

import java.util.List;
import com.guomei.service.curse.CourseVideoService;
import com.guomei.utils.Result;
import com.guomei.utils.ValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 课程视频表Controller
 *
 * @author yourname
 * @date 2025-08-22
 */
@RestController
@RequestMapping("/api1/courseVideo")
@Slf4j
public class CourseVideoController {
    @Resource
    private CourseVideoService courseVideoService;

    /**
     * 新增课程视频
     *
     * @param createOrUpdateCourseVideo 课程视频信息
     * @return 新增结果
     */
    @PostMapping("/addOrUpdate")
    public Result<?> save(@Valid @RequestBody CreateOrUpdateCourseVideo createOrUpdateCourseVideo, BindingResult result) {
        log.info("新增课程视频入参：{}", createOrUpdateCourseVideo);
        //实体类注解参数校验
        Result<?> commonDataResponse = ValidationUtil.validInputParams(result);
        if (commonDataResponse != null) {
            return commonDataResponse;
        }
        CourseVideo courseVideo = new CourseVideo();
        courseVideo.setChapterId(createOrUpdateCourseVideo.getChapterId()); // 修复：设置章节ID
        courseVideo.setCourseId(createOrUpdateCourseVideo.getCourseId());
        courseVideo.setVideoName(createOrUpdateCourseVideo.getVideoName());
        courseVideo.setVideoUrl(createOrUpdateCourseVideo.getVideoUrl());
        courseVideo.setDuration(createOrUpdateCourseVideo.getDuration());
        courseVideo.setSortOrder(createOrUpdateCourseVideo.getSortOrder());
        courseVideo.setIsFree(createOrUpdateCourseVideo.getIsFree());
        try {
            if (createOrUpdateCourseVideo.getId() != null){
                courseVideo.setId(createOrUpdateCourseVideo.getId());
                boolean update = courseVideoService.update(courseVideo);
                return update ? Result.success("更新课程视频成功") : Result.fail("更新课程视频失败");
            } else {
                courseVideo.setStatus(1);
                boolean save = courseVideoService.save(courseVideo);
                return save ? Result.success("新增课程视频成功") : Result.fail("新增课程视频失败");
            }
        } catch (Exception e) {
            log.info("新增课程视频处理失败", e);
            return Result.error("新增课程视频处理失败");
        }
    }

    /**
     * 查询视频详细信息（包含课程和章节的全部字段）
     * @param courseVideo
     * @return 视频详细信息列表
     */
    @PostMapping("/listDetail")
    public Result<?> listDetail(@RequestBody CourseVideo courseVideo) {
        log.info("查询视频详细信息入参：{}", courseVideo);
        try {
            if (courseVideo == null || courseVideo.getPageNum() == null || courseVideo.getPageSize() == null){
                return Result.fail("分页字段信息缺失，查询失败");
            }
            // 验证分页参数是否有效
            if (courseVideo.getPageNum() <= 0 || courseVideo.getPageSize() <= 0) {
                return Result.fail("分页参数无效");
            }
            return courseVideoService.listDetail(courseVideo);
        } catch (Exception e) {
            log.info("查询视频详细信息处理失败", e);
            return Result.error("查询视频详细信息处理失败");
        }

    }

    @PostMapping("/listDetailByIds")
    public Result<?> listDetailByIds(@RequestBody List<Long> ids) {
        log.info("查询视频详细信息入参：{}", ids);
        try {
            if (ids == null || ids.isEmpty()) {
                return Result.fail("ID列表不能为空");
            }
            return courseVideoService.listDetailByIds(ids);
        } catch (Exception e) {
            log.info("查询视频详细信息处理失败", e);
            return Result.error("查询视频详细信息处理失败");
        }
    }

    @PostMapping("/deleteVideo")
    public Result<?> deleteVideo(@RequestBody CourseVideo courseVideo) {
        log.info("删除视频入参：{}", courseVideo);
        try {
            if (courseVideo == null || courseVideo.getId() == null) {
                return Result.fail("视频ID不能为空");
            }
            boolean delete = courseVideoService.removeById(courseVideo.getId());
            return delete ? Result.success("删除视频成功") : Result.fail("删除视频失败");
        } catch (Exception e) {
            log.info("删除视频处理失败", e);
            return Result.error("删除视频处理失败");
        }
    }
}
